#include "AnimatedSprite.h"

void AnimatedSprite::Initialise(HDC* bHDC, LPSTR filename, int xpos, int ypos, int fWidth, int fHeight, float sWidth, float sHeight,
	float animationSpeed, float movementSpeed, float mass, float e, bool hit)
{
	backHDC = bHDC;
	bitmapHDC = CreateCompatibleDC(*backHDC);

	x = xpos;	// starting position
	y = ypos;	// starting position
	frameWidth = fWidth; // Width of the frame
	frameHeight = fHeight; // Hight of the frame
	scaleWidth = sWidth;
	scaleHeight = sHeight;

	RigidBody.Initialise(x, y, frameWidth * scaleWidth, frameHeight * scaleHeight, mass, e, hit);
	frameStepTime = animationSpeed;	// Speed you switch between frames at
	speed = movementSpeed;		// Speed you want the sprite to move at in pixels per second

	LoadABitmap(filename);	// load image
	if(!bitmap)
	{
		//failed to load image
		MessageBox(NULL, "Image fail to load", "Error", MB_OK);
	}

}

void AnimatedSprite::Update(float eTime)
{
	// add delta time to animate elapsed time
	animTime += eTime;
	// if enough time has passed change frame
	if(animTime >= frameStepTime)
	{
		// update offsets
		offsetX += frameWidth;
		if(offsetX > width - frameWidth)
		{
			// offset is outside sprite size
			offsetX = 0;
		}
		// reset animation elapsed time
		animTime = 0;
	}
}

void AnimatedSprite::Move(float eTime)
{
	// if elapsed time is greater than speed 
	// move sprite
	elapsedTime += eTime;
	if(elapsedTime > speed)
	{
		// move sprite
		x++;
		elapsedTime = 0;
	}

}

//transparent background
void AnimatedSprite::drawAnimaSprite(int r, int g, int b)
{
	HBITMAP originalBitMap;

	originalBitMap = (HBITMAP)SelectObject(bitmapHDC, bitmap);
	
	// blt the image to the back buffer
	TransparentBlt(*backHDC, x, y, frameWidth, frameHeight, bitmapHDC, offsetX, offsetY, frameWidth, frameHeight, RGB (r, g, b));

	SelectObject(bitmapHDC, originalBitMap); 
}

//scale the image
void AnimatedSprite::drawAnimaSprite()
{
	HBITMAP originalBitMap;

	originalBitMap = (HBITMAP)SelectObject(bitmapHDC, bitmap);
	
	// blt the image to the back buffer
	TransparentBlt(*backHDC, x, y, frameWidth*scaleWidth, frameHeight*scaleHeight, bitmapHDC, offsetX, offsetY, frameWidth, frameHeight, RGB (255, 0, 255));

	SelectObject(bitmapHDC, originalBitMap); 
}

//rotate image
void AnimatedSprite::drawAnimaSprite(int xpos0, int ypos0, int xpos1, int ypos1, int xpos2, int ypos2)
{
	POINT pt[3];
	//rotates image
	pt[0].x = xpos0;
	pt[0].y = ypos0;
	pt[1].x = xpos1;
	pt[1].y = ypos1;
	pt[2].x = xpos2;
	pt[2].y = ypos2;

	HBITMAP originalBitMap;

	//remember to add the bitmapHDC to scene.h
	originalBitMap = (HBITMAP)SelectObject(bitmapHDC, bitmap);
	
	// blt the image to the back buffer
	PlgBlt(*backHDC, pt, bitmapHDC, offsetX, offsetY, frameWidth, frameHeight, NULL, 0, 0);

	SelectObject(bitmapHDC, originalBitMap); 
}